<?php
session_start();
if(isset($_SESSION['lang']))
    require_once "./language_files/signInServer_" . $_SESSION['lang'] . ".php";
else
    require_once "./language_files/signInServer_en.php";

require_once "statics.php";
if(isset($_POST['email']) && isset($_POST['password']))
{
    $rsPassword=rsSelect("member_id,email,password,activated","member",array("email=",$_POST['email']));
    if(is_string($rsPassword))
    {
        echo $lang['error_1'] . __LINE__;
        exit();
    }
    if(mysql_num_rows($rsPassword) == 0)
    {
        echo "0";
        exit();
    }
    $bRet=bCheckPassword($_POST['email'],$_POST['password']);
    if(is_string($bRet))
    {
        echo $bRet . "ERROR CODE: 0" . __LINE__;
        exit();
    }
    if(!$bRet)
    {
        echo "0";
        exit();
    }
    $row=mysql_fetch_assoc($rsPassword);
    if($row['activated'] == "0")
    {
        echo $lang['error_2'] . __LINE__;
        exit();
    }
    $_SESSION['member_id']=$row['member_id'];
    $_SESSION['email']=$row['email'];
    $_SESSION['password']=$row['password'];
    if(isset($_POST['rememberme']))
    {
        if($_POST['rememberme'] == 1)
        {
            setcookie("cookemail",$_SESSION['email'],time() + 60 * 60 * 24 * 100,"/");
            setcookie("cookpass",$_SESSION['password'],time() + 60 * 60 * 24 * 100,"/");
        }
    }
    else if($_POST['rememberme'] == 0)
    {
        setcookie("cookemail","",time() - 60 * 60 * 24 * 100,"/");
        setcookie("cookpass","",time() - 60 * 60 * 24 * 100,"/");
    }
    echo "1";
}
else if(isset($_POST['confirm_mail']))
{
    $to=$_POST['confirm_mail'];
    $subject=$lang['error_3'];
    $message=$lang['error_4'] . " http://arolis.com/confo/index.php?email=$to&new=TRUE";
    $from="From: postmaster@localhost";
    if(!mail($to,$subject,$message,$from))
    {
        echo $lang['error_5'] . __LINE__;
        exit();
    }
    echo "1";
}
else if(isset($_POST['new_user']))
{
	$salt=substr($_POST['new_user'],0,64);
	$rsNotActivated=rsSelect("*","member",array("activated=",0));
	if(is_string($rsNotActivated))
	{
		echo $rsNotActivated . $lang['error_1'] . __LINE__;
        exit();
	}
	$sEmail="";
	while($row=mysql_fetch_assoc($rsNotActivated))
	{
		$sEmail=$salt.$row['email'];
		for($i=0;$i<5000;$i++)
		{
			$sEmail=hash("sha256",$sEmail);
		}
		$sEmail=$salt.$sEmail;
		if($sEmail===$_POST['new_user'])
		{
			$sEmail=$row['email'];
			break;
		}
		else
		{
			$sEmail="";
		}
	}
	if($sEmail==="")
	{
		echo "Email is not registered in out database" . $lang['error_1'] . __LINE__;
        exit();
	}
    $rsRegistered=rsSelect("*","member",array("email=",$sEmail));
    if(is_string($rsRegistered))
    {
        echo $rsRegistered . $lang['error_1'] . __LINE__;
        exit();
    }
    if(mysql_num_rows($rsRegistered) == 0)
    {
        echo "0";
        exit();
    }
    $iRet=iUpdate("member",array("activated"=>1),array("email=",$sEmail));
    if(is_string($iRet))
    {
        echo $iRet . $lang['error_1'] . __LINE__;
        exit();
    }
    echo "1";
}
function loggedin()
{
    if(isset($_SESSION['email']) || isset($_COOKIE['cookemail']))
    {
        $loggedin=true;
        return $loggedin;
    }
}
?>
